package com.back_end.Array.find;

import java.util.Scanner;

//二分法
public class ErFenFind {
    public static void main(String[] args) {
        int [] arr = new int[]{1,2,3,4,5,6,70,80,90};
        System.out.println("请输入一个数，将在数组中查找：");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        find(arr,n);
    }

    public static void find(int[] arr1, int f){

        int  left = 0;
        int  right = arr1.length-1;
        while(left<=right){
            int middle = (left + right)/2;
            if(f == arr1[middle]){
                System.out.println("找到了，索引为：" + middle);
                break;
            }else if(f < arr1[middle]){
                right = middle - 1;
            }else if(f > arr1[middle]){
                left = middle + 1;
            }
        }
        System.out.println("没找到");
    }
}
